Using the Windows clipboard in Cygwin Vim
Using Vim under Cygwin, it is difficult to access the Windows clipboard: *Typing "+y does not yank (copy) to the Windows clipboard. *Typing "+p does not paste from the Windows clipboard. *Typing "*y and "*p instead seems to work however As a result, you cannot easily use the clipboard to copy text between Cygwin Vim and a native Windows application. This tip shows some workarounds. Copy to Windows clipboard Using this function: function! Putclip(type, ...) range let sel_save = &selection let &selection = "inclusive" let reg_save = @@ if a:type 'n' silent exe a:firstline . "," . a:lastline . "y" elseif a:type 'c' silent exe a:1 . "," . a:2 . "y" else silent exe "normal! `<" . a:type . "`>y" endif "call system('putclip', @@) " if you're using an old Cygwin "call system('clip.exe', @@) " if you're using Bash on Windows "As of Cygwin 1.7.13, the /dev/clipboard device was added to provide "access to the native Windows clipboard. It provides the added benefit "of supporting utf-8 characters which putclip currently does not. Based "on a tip from John Beckett, use the following: call writefile(split(@@,"\n"), '/dev/clipboard') let &selection = sel_save let @@ = reg_save endfunction And simple mappings, such as: vnoremap y :call Putclip(visualmode(), 1) nnoremap y :call Putclip('n', 1) You can visually select some text then yank it to the Windows clipboard, or use the normal yank options (for example, 3\y to yank 3 lines based from the current line, assuming the default backslash leader key). To allow for usage from the command line, add a command to execute Putclip with a range: com! -nargs=0 -range=% Putclip call Putclip('c', , ) Copy using `/dev/clipboard` Exclusively You can eliminate the hassle of a vimscript if you choose, by selecting the text in visual mode, then entering the following... :'<,'>w !cat > /dev/clipboard Then of course you could assign a key combination or whatever you want to the above. I like how explicit this is within the Cygwin environment. Note: The syntax above is `w !` NOT `w!` which do different things. Remember to include the space. Paste from Windows clipboard The easiest way to do this is to press SHIFT-INSERT in insert mode. (The insert key is next to the Delete/Home/End keys.) Another option is to use this function: function! Getclip() let reg_save = @@ "let @@ = system('getclip') "Much like Putclip(), using the /dev/clipboard device to access to the "native Windows clipboard for Cygwin 1.7.13 and above. It provides the "added benefit of supporting utf-8 characters which getclip currently does "not. Based again on a tip from John Beckett, use the following: let @@ = join(readfile('/dev/clipboard'), "\n") setlocal paste exe 'normal p' setlocal nopaste let @@ = reg_save endfunction And a simple mapping, such as: nnoremap p :call Getclip() You can use the normal paste options (for example, \p to paste the clipboard contents, assuming the default backslash leader key). See also * plugin allowing use of "*p, "*yy and more on Cygwin Comments How can we make a "cut" version of copy? I don't know enough about writing functions. Blindly changing "y" to "d" just resulting in it deleting one character only. Robertmarkbram 04:38, 5 August 2009 (UTC) ---- Changing Putclip to be: function! Putclip(type, ...) range let sel_save = &selection let &selection = "inclusive" let reg_save = @@ if a:type 'n' silent exe a:firstline . "," . a:lastline . "d" elseif a:type 'c' silent exe a:1 . "," . a:2 . "d" else silent exe "normal! `<" . a:type . "`>d" endif call system('putclip', @@) let &selection = sel_save let @@ = reg_save endfunction Should do the trick. Ir0nh34d 03:36, 7 August 2009 (UTC) ---- Nice one Ir0nh34d! Changed that to CutClip.. and give it some shortcuts: " Cut via \x in normal or visual mode. vnoremap x :call Cutclip(visualmode(), 1) nnoremap x :call Cutclip('n', 1) " Cut via Alt+x vnoremap ^:call Cutclip(visualmode(), 1) nnoremap ^[x :call Cutclip('n', 1) I like to use Alt+C or \y for copy and Alt+p or \p for paste. That way, I keep some of the Windows and Vim familiarity about the actions. [[User:Robertmarkbram|Robertmarkbram] 08:38, 9 August 2009 (UTC)